package com.dex4d.ui.qr.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.dex4d.BuildConfig;
import com.dex4d.R;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 作者    LiQingyi
 * 时间    2018/11/22 10:37
 * 文件    dex4d-android
 * 描述    地址处理工具类
 */
public class AddressUtils {

    public static final String QR_MD5_KEY_2 = "5tqn5";//对称加密密钥

    public static String convertAddress(String addr) {
        if (addr.length() < 22) {
            return addr;
        } else {
            return addr.substring(0, 12) + "..." + addr.substring(addr.length() - 10, addr.length());
        }
    }

    /**
     * MD5
     *
     * @param string
     * @return
     */
    @NonNull
    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getMD5Key(Context context) {
        StringBuilder builder = new StringBuilder();
        builder.append(BuildConfig.QR_MD5_KEY_1);
        builder.append(QR_MD5_KEY_2);
        builder.append(context.getResources().getString(R.string.qr_md5_key_3));
        builder.append("MvC4k");
        return builder.toString();
    }
}
